<% blazer_title "Checks" %>

<div id="header">
  <div class="pull-right" style="line-height: 34px;">
    <div class="btn-group">
      <%= link_to "New Check", new_check_path, class: "btn btn-info" %>
      <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <span class="caret"></span>
        <span class="sr-only">Toggle Dropdown</span>
      </button>
      <ul class="dropdown-menu">
        <li><%= link_to "Home", root_path %></li>
        <% if Blazer.uploads? %>
          <li><%= link_to "Uploads", uploads_path %></li>
        <% end %>
        <li role="separator" class="divider"></li>
        <li><%= link_to "New Query", new_query_path %></li>
        <li><%= link_to "New Dashboard", new_dashboard_path %></li>
      </ul>
    </div>
  </div>

  <input id="search" type="text" placeholder="Start typing a query or state" style="width: 300px; display: inline-block;" class="search form-control" />
</div>

<table id="checks" class="table">
  <thead>
    <tr>
      <th>Query</th>
      <th style="width: 10%;">State</th>
      <th style="width: 10%;">Run</th>
      <th style="width: 20%;">Notify</th>
      <th style="width: 15%;"></th>
    </tr>
  </thead>
  <tbody>
    <% @checks.each do |check| %>
      <tr>
        <td><%= link_to check.query.name, check.query %> <span class="text-muted"><%= check.try(:check_type).to_s.gsub("_", " ") %></span></td>
        <td>
          <% if check.state %>
            <small class="check-state <%= check.state.parameterize.gsub("-", "_") %>"><%= check.state.upcase %></small>
          <% end %>
        </td>
        <td><%= check.schedule if check.respond_to?(:schedule) %></td>
        <td>
          <ul class="list-unstyled" style="margin-bottom: 0; word-break: break-all;">
            <% check.split_emails.each do |email| %>
              <li><%= email %></li>
            <% end %>
            <% check.split_slack_channels.each do |channel| %>
              <li><%= channel %></li>
            <% end %>
          </ul>
        </td>
        <td style="text-align: right; padding: 1px;">
          <%= link_to "Edit", edit_check_path(check), class: "btn btn-info" %>
          <%= button_to "Run Now", refresh_query_path(check.query), class: "btn btn-primary" %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= javascript_tag nonce: true do %>
  $("#search").on("keyup", function() {
    var value = $(this).val().toLowerCase()
    $("#checks tbody tr").filter( function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    })
  }).focus()
<% end %>
